// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: jhkim

#pragma once

#include <iostream>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <string>

#include <boost/algorithm/string.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include "coin2/exchange/base/symbology/spot_product.h"
#include "coin2/exchange/base/symbology/util.h"
#include "coin2/exchange/binance/symbology/currency.h"

namespace coin2::exchange::binance::symbology {

using BinanceCurrency = coin2::exchange::binance::symbology::BinanceCurrency;

class BinanceProduct
    : public base::symbology::SpotProduct<BinanceCurrency, BinanceProduct>,
      public base::symbology::ProductFromStrMixin<BinanceProduct> {
 public:
  typedef base::symbology::SpotProduct<BinanceCurrency, BinanceProduct> BaseProduct;

 public:
  BinanceProduct(
      const BinanceCurrency& base,
      const BinanceCurrency& quote)
      : SpotProduct(
            base,
            quote,
            ToBinanceNativeProduct(base, quote),
            ToBinanceFeedNativeProduct(base, quote),
            base::market::ExchangeType::Binance) {}

 public:
  static std::unique_ptr<BinanceProduct> FromNativeStrImpl(
      const std::string& native_product,
      int64_t resolve_timestamp) {
    std::unique_ptr<BinanceCurrency> base_ptr;
    std::unique_ptr<BinanceCurrency> quote_ptr;
    bool success = ParseBinanceNativeProduct(
        native_product, resolve_timestamp, &base_ptr, &quote_ptr);
    if (success) {
      return std::make_unique<BinanceProduct>(*base_ptr, *quote_ptr);
    } else {
      return { };
    }
  }
  // Binance Nav feed native symbol use upper case of base currency.
  static std::unique_ptr<BinanceProduct> FromNavNativeProductNoThrow(
      const std::string& native_product,
      int64_t resolve_timestamp) {
    std::string nav_type = "";
    for (auto _nav_type : {"UP", "DOWN"}) {
      if (boost::algorithm::ends_with(native_product, _nav_type)) {
        nav_type = _nav_type;
        break;
      }
    }
    if (nav_type.empty()) {
      NOTREACHED();
    }

    auto binance_currency = BinanceCurrency::FromNativeStrNoThrow(
        native_product, resolve_timestamp);
    if (!binance_currency) {
      return {};
    }

    return std::make_unique<BinanceProduct>(
      *binance_currency,
      *BinanceCurrency::FromNativeStrNoThrow("USDT", resolve_timestamp));
  }
  const std::string& nav_native_symbol() const {
    return base().currency();
  }

 private:
  static bool ParseBinanceNativeProduct(
      const std::string& product_str,
      int64_t resolve_timestamp,
      std::unique_ptr<BinanceCurrency>* base_ptr,
      std::unique_ptr<BinanceCurrency>* quote_ptr) {
    using base::symbology::HasValue;
    static const std::string quotes[] = {
        "AUD", "BIDR", "BNB", "BRL", "BTC", "BUSD", "DAI",
        "DOGE", "ETH", "EUR", "GBP", "IDRT", "NGN", "RUB",
        "TRX", "TRY", "TUSD", "UAH", "USDC", "USDP", "USDT",
        "UST", "VAI", "XRP", "PAX", "DOT", "PLN", "RON", "ZAR", "ARS", "FDUSD"
    };

    const std::string& product_str_upper = StringToUpperCopy(product_str);

    for (const auto& quote_cand : quotes) {
      if (StringEndsWith(product_str_upper, quote_cand)) {
        size_t base_len = product_str_upper.size() - quote_cand.size();
        std::string base = product_str_upper.substr(0, base_len);
        *base_ptr = BinanceCurrency::FromNativeStrNoThrow(base, resolve_timestamp);
        *quote_ptr = BinanceCurrency::FromNativeStrNoThrow(quote_cand, resolve_timestamp);
        return true;
      }
    }
    return false;
  }

  static std::string ToBinanceNativeProduct(
      const BinanceCurrency& base,
      const BinanceCurrency& quote) {
    using base::market::MarketType;
    std::stringstream ss;
    ss << base.native_currency(MarketType::Spot)
        << quote.native_currency(MarketType::Spot);
    return ss.str();
  }

  static std::string ToBinanceFeedNativeProduct(
      const BinanceCurrency& base,
      const BinanceCurrency& quote) {
    return StringToLowerCopy(ToBinanceNativeProduct(base, quote));
  }
};

}  // namespace coin2::exchange::binance::symbology
